package com.atjinggu.jinggumail.seckill.schedule;

import com.atjinggu.jinggumail.common.utils.R;
import com.atjinggu.jinggumail.seckill.feign.CouponFeignService;
import com.atjinggu.jinggumail.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author jingming
 * @Type SeckKillSkuScheduled.java
 * @Desc 秒杀商品上架：
 *    最近三天的秒杀商品上架：
 *
 * @date 2021/3/23 11:50
 */
@Service
@Slf4j
public class SecKillSkuScheduled {

   @Autowired
    SeckillService seckillService;

   @Autowired
    RedissonClient redissonClient;

   public static final String UPLOAD_SECKILL_LOCK =  "upload:seckill:lock";

   @Scheduled(cron = "0 */1 * * * ?")
    public void uploadSeckillSku(){
        //重复无需处理，因为上一次已经上架了
       RLock lock = redissonClient.getLock(UPLOAD_SECKILL_LOCK);
       lock.lock(10, TimeUnit.SECONDS);
       try {
           log.info("商品开始上架！");
           seckillService.list3DaysSeckillSku();
           log.info("商品开始上架结束！");
       }finally {
           lock.unlock();
       }


    }
}